﻿
function loadhtml(target,url, body) 
{
  $.ajax(
  {
    type: "post",
    dataType: "html",
    url: url,
    data: body,
    success: function(result, textStatus) {
      hideWait(target);
      $(target).html(result);
    },
    error: function(xhr, textStatus, errorThrown) {
      if (xhr.status != 401 && xhr.status != 403) {
        alert('Error')
      }
    }
  });
  showWait(target);
  return false;
}

function redirect(link) {
  document.location = link
}

function setTableMultiSelect()
{
    $('.table tr')
    .filter(':has(:checkbox:checked)')
    .addClass('selected')
    .end()
//    .not($('#checkAll').parents('tr:first'))
//    .click(function(event) {
//        if (event.target.nodeName == 'TD') {
//            $(':checkbox', this).trigger('click');
//        }
//    })
    .find(':checkbox')
    .click(function(event) {
        $(this).parents('tr:first').toggleClass('selected');        
    });  
}

function editStoryOk(id)
{
  $.ajax(
  {
      type: "post",
      dataType: "html",
      url: "/ProductBacklog/Update",
      data: $('#form'+id).serialize(),
      success: function(result, textStatus)
      {   
        $("#editStory"+id).unbind("keydown","Esc", function(){ editStoryCancel(id); });
        $("#editStory"+id).unbind("keydown","alt+return", function(){ editStoryOk(id);});                                 
        if(result.indexOf("editStory"+id) != -1)
        {
          //validation error          
          $("#editStory"+id).replaceWith(result);          
          $("#editStory"+id).bind("keydown","Esc", function(){ editStoryCancel(id); });
          $("#editStory"+id).bind("keydown","alt+return", function(){ editStoryOk(id);});                                      
        }      
        else
        {
          $('#'+id).remove();          
          $(result).insertAfter("#editStory"+id);
          $("#editStory"+id).remove();                    
        }
      },
      error: function(xhr, textStatus, errorThrown) {
        if (xhr.status != 401 && xhr.status != 403) 
        {
          alert('Error')  
        }
      }
  });
  showWait('#editStory' + id);
  return false;
} 
    
function editStory(id)
{    
  $.ajax(
  {
      type: "post",
      dataType: "html",
      url: '/ProductBacklog/Edit',
      data: 'id='+id,   
      success: function(result, textStatus) {       
        $('#'+id).hide();
        $(result).insertAfter("#"+id);
        $("#editStory"+id+" input:first").focus();  
        $("#editStory"+id).bind("keydown","Esc", function(){ editStoryCancel(id); });
        $("#editStory"+id).bind("keydown","alt+return", function(){ editStoryOk(id);});          
        hideWait('#'+id);        
      },
      error: function(xhr, textStatus, errorThrown) {
        if (xhr.status != 401 && xhr.status != 403) 
        {
          alert('Error')  
        }
      }
  }); 
  showWait('#'+id);
  return false;
}

function deleteStories()
{  
  if($("#tableForm input:checked").length == 0)
    return false;
    
  if(confirm ("Are you sure, remove "+$("#tableForm input:checked").length+" elements ?"))
  {
    $.ajax(
    {
      type: "post",
      dataType: "html",
      url: '/ProductBacklog/Delete',
      data: $('#tableForm').serialize(),
      success: function(result, textStatus) {
        $('#table :checked').each(function() {
          $('#' + $(this).val()).remove();
        });      
      },
      error: function(xhr, textStatus, errorThrown) {
        if (xhr.status != 401 && xhr.status != 403) {
          alert('Error')
        }      
      }
    });       
  }
  return false;
}

function showWait(id)
{
  $(id).block(
  { 
    message: "loading...",  
    overlayCSS: 
    { 
      backgroundColor: "#fff", 
      opacity : 0.0
    }   
  }); 
}

function hideWait(id)
{
  $(id).unblock(); 
}

function editStoryCancel(id)
{
  $("#editStory"+id).remove();
  $('#'+id).show();
  return false;
}

function addStory()
{  
  if($("#newStory") != null)
  {  
    $.ajax({
        type: "post",
        dataType: "html",
        url: '/ProductBacklog/New',
        success: function(result, textStatus) 
        {
          $("#sortable").prepend(result);
          $("#newStory").bind("keydown","Esc", addStoryCancel);
          $("#newStory").bind("keydown","alt+return", addStoryOk);
          $("#newStory input:first").focus();    
        },
        error: function(xhr, textStatus, errorThrown) {
          if (xhr.status != 401 && xhr.status != 403) 
          {
            alert('Error')  
          }
        }
    });
  }
  return false;
}

function addStoryOk()
{  
  $.ajax({
      type: "post",
      dataType: "html",
      url: '/ProductBacklog/Insert',
      data: $('#newStoryForm').serialize(),
      success: function(result, textStatus) 
      {
        hideWait('#newStory');
        $("#newStory").unbind("keydown","Esc", addStoryCancel);
        $("#newStory").unbind("keydown","alt+return", addStoryOk);   
        $("#newStory").replaceWith(result);                        
        $("#newStory input:first").focus();                
        $("#newStory").bind("keydown","Esc", addStoryCancel);
        $("#newStory").bind("keydown","alt+return", addStoryOk);
      },
      error: function(xhr, textStatus, errorThrown) {
        if (xhr.status != 401 && xhr.status != 403) 
        {
          alert('Error')  
        }
      }
  });
  showWait('#newStory');
  return false; 
}

function addStoryCancel()
{
  $("#newStory").unbind("keydown","Esc", addStoryCancel);
  $("#newStory").unbind("keydown","alt+return", addStoryOk);    
  $("#newStory").remove();      
  return false;
}

function confirmDelete(message, url) 
{
  if(confirm(message))
  {
    document.location = url;
  }
  return false;
}

function confirmDeleteForm(message, url, form) {
  var count = $("#" + form + " input:checked").length;
  if (count == 0)
    return false;
    
  if (confirm(message.replace("{0}",count))) {
    $('#'+form).attr('action',url);
    $('#'+form).submit();
  }
  return false;
}


